package com.example.light.web.personal.controller;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.common.result.Result;
import com.example.light.web.personal.service.PersonalService;
import com.example.light.web.personal.vo.UserVO;
import com.example.light.web.system.entity.User;
import com.example.light.web.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("personal")
@Controller
public class PersonalController {

    @Autowired
    private UserService userService;
    @Autowired
    private PersonalService personalService;


    @RequestMapping("show")
    public String showPersonalInfo(Model model, User user) {
        UserVO userVO = personalService.showUserVO(user);
        model.addAttribute("personalUser", userVO);
        return "personal/profile";
    }

    /**
     * @param file
     * @param userVO
     * @param result BindingResult 接收前端form表单name属性
     * @return
     */
    @RequestMapping("modify")
    @ResponseBody
    public Result<Boolean> modifyPersonalInfo(HttpServletRequest request
            , @RequestParam(value = "file", required = false) MultipartFile file, UserVO userVO, BindingResult result) {
        if (file == null || file.getOriginalFilename().equalsIgnoreCase("")) {
            throw new GlobalException(CodeMsg.PersonalCodeMsg.FILE_HEAD_NULL);
        }
        personalService.updateUserInfo(userVO, request, file);
        return Result.success(true);
    }
}
